package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.yc.sole.common.constant.PubConstant;
import com.yc.sole.common.enums.PositionState;
import com.yc.sole.erp.dto.EmployeeDto;
import com.yc.sole.erp.repository.EmployeeRepository;
import com.yc.sole.erp.request.EmployeeQueryReq;
import com.yc.sole.erp.request.EmployeeReq;
import com.yc.sole.erp.response.EmployeeResp;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.service.SequenceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-04
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class EmployeeBiz {

    private final EmployeeRepository employeeRepository;
    private final SequenceService sequenceService;

    /**
     * 新增
     *
     * @param employeeReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveEmployee(EmployeeReq employeeReq) {
        EmployeeDto dto = new EmployeeDto();
        BeanUtil.copyProperties(employeeReq, dto);

        String employeeNo = sequenceService.generateSeqNo(PubConstant.EMPLOYEE_NO);
        dto.setEmployeeNo(employeeNo);
        employeeRepository.saveEmployee(dto);
    }

    /**
     * 修改
     *
     * @param employeeReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateEmployeeById(EmployeeReq employeeReq) {
        EmployeeDto dto = new EmployeeDto();
        BeanUtil.copyProperties(employeeReq, dto);
        if (PositionState.LEAVE_OFFICE.equals(dto.getPositionState())) {
            dto.setDepartureDate(DateUtil.today());
        } else {
            dto.setDepartureDate(null);
        }
        employeeRepository.updateEmployeeById(dto);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmployee(Collection<Long> ids) {
        employeeRepository.deleteEmployeeByIds(ids);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param employeePageReq
     * @return
     */
    public Paging<EmployeeResp> selectEmployeePage(Paging<EmployeeQueryReq> page, EmployeeQueryReq employeePageReq) {
        Paging<EmployeeDto> paging = employeeRepository.selectEmployeePage(page, employeePageReq);
        return PageUtil.pageCopy(paging, EmployeeResp.class);
    }

    /**
     * 根据员工名称查询
     *
     * @param employeeQueryReq
     * @return
     */
    public List<EmployeeResp> selectEmployeeByName(EmployeeQueryReq employeeQueryReq) {
        return BeanUtil.copyToList(employeeRepository.selectEmployeeByName(employeeQueryReq.getEmployeeName()), EmployeeResp.class);
    }

    /**
     * 根据id查询员工
     *
     * @param employeeId
     * @return
     */
    public EmployeeResp selectEmployeeById(Long employeeId) {
        return BeanUtil.copyProperties(employeeRepository.selectEmployeeById(employeeId), EmployeeResp.class);
    }
}
